/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package famtime.tm.kalender.presentation;

import famtime.tm.kalender.business.KalenderService;
import famtime.tm.kalender.model.Event;
import famtime.tm.kalender.model.FamUser;
import famtime.tm.kalender.model.Kalender;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Marius
 */
@Named
@SessionScoped
public class KalenderPM implements Serializable {

    
    
    @EJB
    private KalenderService service;
    private String username, password;
    private String kalenderName;
    private FamUser user;
    private Date actualDateBiatch;

    /**
     *
     * @throws IOException
     */
    @PostConstruct
    private void init() throws IOException{
        if(!isLoggedIn()) {

        }
        else
        {
            actualDateBiatch = new Date();
        }
    }
    
    public List<Event> findEventsByDate()
    {
        
    return null;
    }
    
    public void addKalender() {
        System.out.println("CreatingObject with:" + this.kalenderName);
        Kalender kalender = new Kalender(kalenderName);
        kalender.setName(kalenderName);
        service.create(kalender);
        kalenderName = "";
    }

    public List<Kalender> getKalenders() {
        return service.findAll();
    }
    
    public void login() throws IOException{
        user = service.login(username,password);
        this.username = "";
        this.password = "";
        if(user != null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        }
    }
    
    public void register() throws IOException{
        
    }
    
    public void logout() throws IOException{
        this.user = null;
        FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
    }
    
    public boolean isLoggedIn() {
       return this.user != null;
    }

    //Getter+Setter
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }


    public String getKalenderName() {
        return kalenderName;
    }

    public void setKalenderName(String kalenderName) {
        this.kalenderName = kalenderName;
        System.out.println("setKalenderName: " + this.kalenderName);
    }
}
